﻿using PLC.Core.Entities;
using PLC.Interface.RecipeVariables;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YWM.Application;
using YWM.Application.Linq.Extensions;

namespace PLC.Application.RecipeVariables.Services
{
    internal class RecipeVariableService : AsyncCrudService<RecipeVariable, RecipeVariableDto, long, RecipeVariableResultRequestDto>, IRecipeVariableService
    {
        protected override IQueryable<RecipeVariable> CreateFilteredQuery(RecipeVariableResultRequestDto input)
        {
            IQueryable<RecipeVariable> queryable = base.CreateFilteredQuery(input)
                .WhereIf(input.RecipeId != null, rv => rv.RecipeId == input.RecipeId);
            return queryable;
        }

        public async Task Delete(long recipeId)
        {
            List<RecipeVariable> recipeVariables = await Repository.GetAllListAsync(rv => rv.RecipeId == recipeId);
            foreach (RecipeVariable recipeVariable in recipeVariables)
            {
                await Repository.DeleteAsync(recipeVariable);
            }
        }
    }
}
